package com.atguigu.controller;

import com.atguigu.entity.BaseBrand;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.service.BaseBrandService;
import com.atguigu.service.BaseCategoryViewService;
import com.atguigu.service.SkuDetailService;
import com.atguigu.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.entity.SkuInfo;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA2020.
 * User: yangchao
 * Date: 2021/11/20:19:58
 * Description:
 */
@RestController
@RequestMapping("/sku/")
public class SkuDetailController {
    @Autowired
    private SkuDetailService skuDetailService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private BaseCategoryViewService categoryViewService;
    @Autowired
    private BaseBrandService brandService;
    @Resource
    private  RedisTemplate redisTemplate;


    //1.根据skuid查询sku_info信息  包括图片
    @GetMapping("getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable Long skuId){
        SkuInfo skuInfo =skuDetailService.getSkuInfo(skuId);
        return skuInfo;
    }

    //2.根据skuId查询spu的销售属性key与value
    @GetMapping("getSpuSaleProperty/{productId}/{skuId}")
    public List<ProductSalePropertyKey> getSpuSaleProperty(@PathVariable long productId, @PathVariable long skuId) {

//        String key ="productId:"+productId+"|"+"skuId:"+skuId;
//        List<ProductSalePropertyKey> list;
//        list =(List<ProductSalePropertyKey>) redisTemplate.opsForValue().get(key);
//        if(list!=null){
//            return list;
//
//        }else{
//            list =skuDetailService.getSpuSaleProperty(productId,skuId);
//            redisTemplate.opsForValue().set(key,list);
//            return list;
//       }

        List<ProductSalePropertyKey> list=skuDetailService.getSpuSaleProperty(productId,skuId);
        return list;


    }

    //3.根据skuId 查询商品的价格
    @GetMapping("getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable long skuId){

//        String key ="price:"+skuId;
//        BigDecimal price;
//        price =(BigDecimal) redisTemplate.opsForValue().get(key);
//        if(price!=null){
//            return price;
//
//        }else{
//            price =skuInfoService.getById(skuId).getPrice();
//            redisTemplate.opsForValue().set(key,price);
//            return price;
//        }
        BigDecimal price=skuInfoService.getById(skuId).getPrice();
        return price;
    }
    //4.获取sku的分类信息
    @GetMapping("getCategoryView/{category3Id}")
    public BaseCategoryView getCategoryView(@PathVariable long category3Id){
//        String key ="getCategoryView:"+category3Id;
//        BaseCategoryView getCategoryView;
//        getCategoryView =(BaseCategoryView) redisTemplate.opsForValue().get(key);
//        if(getCategoryView!=null){
//            return getCategoryView;
//
//        }else{
//            getCategoryView =categoryViewService.getById(category3Id);
//            redisTemplate.opsForValue().set(key,getCategoryView);
//            return getCategoryView;
//        }

        BaseCategoryView getCategoryView = categoryViewService.getById(category3Id);
        return getCategoryView;

    }
    //5.通过spuId 获取对应的Json 字符串。
    @GetMapping("getSkuSalePropertyValueIdMapping/{productId}")
    public Map getSkuSalePropertyValueIdMapping(@PathVariable long productId){

//        String key ="Mapping:"+productId;
//        Map Mapping;
//        Mapping =(Map) redisTemplate.opsForValue().get(key);
//        if(Mapping!=null){
//            return Mapping;
//
//        }else{
//            Mapping =skuDetailService.getSkuSalePropertyValueIdMapping(productId);
//            redisTemplate.opsForValue().set(key,Mapping);
//            return Mapping ;
//        }

        Map Mapping = skuDetailService.getSkuSalePropertyValueIdMapping(productId);
        return Mapping;
    }
    //6.通过brandId查询品牌信息
    @GetMapping("getBrandById/{brandId}")
    public BaseBrand getBrandById(@PathVariable Long brandId){
        return brandService.getById(brandId);
    }


}
